package com.haizhi.crm.recommend.context;

import com.haizhi.crm.recommend.service.RecService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Repository;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by chengmo on 2018/6/28.
 */
@Repository("recRegister" )
public class RecRegister implements InitializingBean, ApplicationContextAware {
    private static Map<String, RecService> REC_SERVICES = new ConcurrentHashMap<>();
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, RecService> serviceMap = applicationContext.getBeansOfType(RecService.class);
        for (RecService service : serviceMap.values()) {
            REC_SERVICES.put(service.getName(), service);
        }
    }

    public static RecService getServiceImpl(String name) {
        return REC_SERVICES.get(name);
    }
}
